<?php
/*
Template Name: Inserisci Preziosi
*/
session_start();
include('classe_resize.php');
?>
<script type="text/javascript">
function numeralsOnly(evt) {
/*
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
  if (charCode > 31 && (charCode < 46 || charCode > 57)) {
    alert("Puoi inserire solo numeri e il punto decimale!");
    return false;
  }
 */
  return true;
}

function invia(cat){
	window.location = 'http://<?php echo $_SERVER['HTTP_HOST'].$_SERVER['REDIRECT_URL'];?>?categoria='+cat;
}

function check_file(){
        str=document.getElementById('datasheet').value.toUpperCase();
        suffix=".DOC";
        suffix2=".PDF";
        if(str.indexOf(suffix, str.length - suffix.length) == -1||
                       str.indexOf(suffix2, str.length - suffix2.length) == -1){
        alert('File type not allowed,\nAllowed file: *.doc,*.pdf');
            document.getElementById('datasheet').value='';
        }
    }
</script>
<style type="text/css">
<!--
.Stile31 {font-size: 14px}
.Stile32 {font-size: 14px; color: #FFFFFF; }
.Stile33 {color: #FFFFFF}
.Stile35 {font-size: 16px;
	font-weight: bold;
	color: #FFFFFF;
}
.Stile36 {font-size: 14px; color: #333333; }
-->
</style>
<?php get_header(); global $wpdb; ?>

			<?php get_sidebar('top'); ?>
			<table width="100%" border="0" cellspacing="0" cellpadding="5">
  				<tr>
    				<td colspan="0" valign="top"><font color="#193E52" size="+2" face="Arial, Helvetica, sans-serif"><b><?php echo theme_get_meta_option($post->ID, 'theme_show_page_title') ? get_the_title() : ''?></b></font></td>
  				</tr>
			</table>
<?php
if (is_user_logged_in()) {
$path = WP_CONTENT_DIR."/uploads/";
function Upload_Resize($path,$immagine,$tmp_img){  
	$error['flag'] = FALSE;
	$error['msg'] = "";
	$uploads = wp_upload_bits($immagine,NULL ,file_get_contents($tmp_img));
	if(!($uploads['error'] == FALSE)){ 
		//print_r($uploads);
		$error['flag'] = TRUE;
		$error['msg'] = "wp_upload_bits error<br>";
		return $error;
	}
	//echo 'file = '.$uploads['file'].'======<br>';
	//echo 'url  = '.$uploads['url']. '======<br>';
	//echo 'dest = '.$path. "lista/lista/".$immagine;
	$moveResult = rename( $uploads['file'] , $path."lista/lista/".$immagine );
	if ($moveResult == FALSE){
		$error['flag'] = TRUE;
		$error['msg'] = "rename file error";
		return $error;
	}	
	$resize = new resize;
	$resize->urlimage = $path."lista/lista/".$immagine;
	$resize->fisso = 0;
	$resize->maxX = 600;
	$resize->maxY = 600;
	$resize->latofisso = "X";
	$resize->folder = $path."lista/lista/thumbnails/";
	$resize->newName = "thumb_".$immagine;
	//
	$size = getimagesize($resize->urlimage);
	//echo '<br>$size[0]*$size[1]*4 = '.$size[0]*$size[1]*4 .'<br>';
	//echo '<br>memory_get_peak_usage()= '.memory_get_peak_usage().'<br>';
	if (($size[0]*$size[1]*4)>80000000){
			//echo "<script>alert('image to big = ".($size[0]*$size[1]*4)>8000000). "!!')</script>";
			$resize->errore[0] = 'image to big = '.($size[0]*$size[1]*4).'>80000000 !!';
		}else{
			$resize->go();
			unset($resize);
			$resize = new resize;
			$resize->urlimage = $path."lista/lista/".$immagine;
			$resize->fisso = 0;
			$resize->maxX = 600;
			$resize->maxY = 600;
			$resize->latofisso = "X";
			$resize->folder = $path."lista/lista/";
			$resize->newName = "grande_".$immagine;
			//$resize->go();
			//@unlink($path."lista/lista/".$immagine);
			$moveResult = rename( $path."lista/lista/".$immagine , $path."lista/lista/grande_".$immagine );
		}	
	//	
	
	if(count($resize->errore)==0){
		/*
		echo "Nome file originale: ".$resize->originalName."<br>";
		echo "Altezza file originale: ".$resize->originalY."<br>";
		echo "Larghezza file originale: ".$resize->originalX."<br>";
		echo "Nuova Altezza: ".$resize->newY."<br>";
		echo "Nuova Larghezza: ".$resize->newX."<br>";
		*/
	}else{
		$error['flag'] = TRUE;
		$error['msg'] = implode('<br>', $resize->errore);
		return $error;
	}
}

if($_POST['upload'] == "Conferma"){
	$foto       = $_FILES['image']['name'];
	$caratteri  = array(" ","'","(",")","[","]","/","_");
	$foto       = str_replace($caratteri,"",$foto);
	$foto       = strtolower($foto);
	
	$code           = $_POST['code'];
	$tipo           = $_POST['categoria'];
	$varietà        = $_POST['sottocategoria'];
	$certificato    = $_POST['certificato'];
	$nome_cert		= $_POST['nome_cert']!=''?$_POST['nome_cert']:'';
	$carati         = $_POST['carati'];
	$taglio         = $_POST['taglio'];
	$forma          = $_POST['forma'];
	$altezza        = $_POST['altezza'];
	$lunghezza      = $_POST['lunghezza'];
	$profondità     = $_POST['profondità'];
	$origine        = $_POST['origine'];
	$note           = $_POST['note'];
	$prezzo         = $_POST['prezzo'];
    $currency       = $_POST['currency'];
    $price_type     = $_POST['price_type'];
	$datasheet_name = $_FILES['datasheet']['name'];
	$datasheet_link = "/datasheet/".$_FILES['datasheet']['name'];;
	$v = array(
		'tipo'          => $tipo,
		'varietà'       => $varietà,
		'certificato'   => $certificato,
		'carati'        => $carati,
		'taglio'        => $taglio,
		'forma'         => $forma,
		'altezza'       => $altezza,
		'lunghezza'     => $lunghezza,
		'profondità'    => $profondità,
		'origine'       => $origine,
		'note'          => $note,
		'prezzo'        => $prezzo,
		'foto'          => $foto,
		'code'          => $code,
                'currency'      => $currency,
                'price_type'    => $price_type,
		'datasheet_link'	=> 	$datasheet_link	
	);
	if ( $foto == ""){ 
		$msg = 'Nome file FOTO non selezionato!!';
	}else{
		$error = Upload_Resize($path,$foto,$_FILES['image']['tmp_name']);
		if ( $error['flag'] == TRUE)
				$msg = $error['msg'];	
		}	
	if ($msg != '')
		echo "<script>alert('".$msg."')</script>";

if ( $datasheet_name == ""){ 
		$msg = 'Nome file "foglio caratteristiche" non selezionato!!';
	}else{
		$datasheet_link = $_FILES['datasheet']['name'];
		$error = move_uploaded_file($_FILES['datasheet']['tmp_name'], $path."/datasheet/".$_FILES['datasheet']['name']);
		
		
		if ( !$error) { 
			$msg = $_FILES['datasheet']['tmp_name'].'error upload';
			echo "<script>alert('".$msg."')</script>";	
			}
		}
//print_r ($v);
if($wpdb->insert($wpdb->prefix.'preziosi',$v)){ 
		$msg = "Inserimento datasheet nel database eseguito correttamente!";
	}else{
			$msg = "errore database!";
			}

	echo "<script>alert('".$msg."')</script>";
}

?>
<form action="<?php $_SESSION['PHP_SELF']; ?>" name="form" method="post" enctype="multipart/form-data">
<table width="100%"  border="0" cellpadding="2" cellspacing="1" style="border:0px">
    <tr>
		<td width="150" height="21" background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Categoria: </span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        	<select name="categoria" id="categoria" onchange="invia(this.value)" >
            <?php
				$categorie_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}cat_preziosi ORDER BY id");
				foreach ($categorie_sql as $categorie){
					if( (isset($_GET['categoria'])) ){
						if($categorie->id==$_GET['categoria']){
							echo "<option value=".$categorie->id." selected=\"selected\">".ucfirst($categorie->nome)."</option>";
						}else{
							echo "<option value=".$categorie->id.">".ucfirst($categorie->nome)."</option>";
						}
					}else{
						echo "<option value=".$categorie->id.">".ucfirst($categorie->nome)."</option>";
					}
				}
			?>
			</select>
        	</span></td>
  </tr>
  <tr>
    <td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Sottocategoria: </span></td>
    <td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
		<select name="sottocategoria">
			<?php
				if(isset($_GET['categoria'])){
					$categorie=$_GET['categoria'];
					$subcat_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}subcat_preziosi WHERE categoria = '$categorie' ORDER BY id");
				}else{
					$categoria = 1;
					$subcat_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}subcat_preziosi WHERE categoria = '$categoria' ORDER BY id");
				}
					
				foreach($subcat_sql as $subcat){
					if($sottocategoria == $subcat->id)
						echo "<option value=".$subcat->id." selected=\"selected\">".ucfirst($subcat->nome)."</option>";
					else
						echo "<option value=".$subcat->id.">".ucfirst($subcat->nome)."</option>";
				}
			?>
		</select>
        </span></td>
	</tr>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Code</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <input type="text" name="code" size="71"/>
        </span></td>
	</tr>      
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">FOTO</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <input type="file" name="image">
        </span></td>
	</tr>    
    <!--tr>
		<td bgcolor="#174E9B"><span class="Stile32" style="margin-left:5px">Nome Italiano</span></td>
		<td bgcolor="#174E9B"><input type="text" name="nome_ita" value="<?php //echo $nome_ita; ?>" /></td>
	</tr>
	<tr>
		<td bgcolor="#247CD7"><span class="Stile32" style="margin-left:5px">Nome Inglese</span></td>
		<td bgcolor="#247CD7"><input type="text" name="nome_eng" value="<?php //echo $nome_eng; ?>" /></td>
	</tr>
	<tr>
		<td bgcolor="#174E9B"><span class="Stile32" style="margin-left:5px">Tipo</span></td>
		<td bgcolor="#174E9B"><input type="text" name="tipo" value="<?php //echo $tipo; ?>" /></td>
	</tr>
	<tr>
		<td bgcolor="#247CD7"><span class="Stile32" style="margin-left:5px">Varietà</span></td>
		<td bgcolor="#247CD7"><input type="text" name="varietà" value="<?php //echo $varietà; ?>" /></td>
	</tr-->
    <?php if(!isset($_GET['categoria'])||$_GET['categoria']!=3){ ?>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Certificato</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        	<input type="radio" name="certificato" value="yes" size="71"/><span class="Stile33"><span class="Stile36" style="margin-left:2px">SI </span>
        	<input type="radio" name="certificato" value="no" size="71"/><span class="Stile36" style="margin-left:2px">NO</span></span>
        	<input type="text" name="nome_cert" style="display:none"/>
        </span></td>
	</tr>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Carati</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <input type="text" name="carati" size="71" onkeypress="return numeralsOnly(event)"/>
        </span></td>
	</tr>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
        	<span class="Stile36" style="margin-left:2px">Taglio</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid">
        	<span class="Stile36" style="margin-left:2px"><input type="text" name="taglio" size="71"/></span>
        </td>
	</tr>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Forma</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <input type="text" name="forma" size="71"/>
        </span></td>
	</tr>
    <?php } ?>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Dimensioni</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
		  <table>
				<tr>
					<td width="65"><input type="text" name="altezza" size="20" onkeypress="return numeralsOnly(event)"/></td>
                    <td><span class="Stile36" style="margin-left:2px">X</span></td>
					<td width="79"><input type="text" name="lunghezza" size="20" onkeypress="return numeralsOnly(event)"/></td>
                    <td><span class="Stile36" style="margin-left:2px">X</span></td>
					<td width="117"><input type="text" name="profondità" size="20" onkeypress="return numeralsOnly(event)"/></td>
					<td><span class="Stile36" style="margin-left:2px">mm</span></td>
				</tr>
			</table>
		</span></td>
	</tr>
    <?php if(!isset($_GET['categoria'])||$_GET['categoria']!=3){ ?>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Origine</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <input type="text" name="origine" size="71"/>
        </span></td>
	</tr>
    <?php } ?>
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">Note</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <textarea name="note" rows="5" cols="71"></textarea>
        </span></td>
	</tr>      
	<tr>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">foglio caratteristiche</span></td>
		<td background="<?php bloginfo('template_url');?>/images/bgtab3.png" style="border-bottom:#666666; border-bottom-width:1px; border-bottom-style:solid"><span class="Stile36" style="margin-left:2px">
        <input type="file" name="datasheet" accept=".pdf,.doc,.docx"/> 
        </span></td>
	</tr>    
	<tr>
		<td height="24" bgcolor="#174E9B">
                    <!--span class="Stile35" style="margin-left:3px">Prezzo per carato</span-->
                    <select name="price_type">
                        <option value="tot">Per carato</option>
                        <option value="cad">Totale</option>
                    </select>
                </td>
		<td bgcolor="#174E9B"><span class="Stile35" style="margin-left:3px"> 
        <input type="text" name="prezzo" onkeypress="return numeralsOnly(event)" size="55" style="float:left; width: 550px"/>
        <select id="currency" name="currency"><option value="€">€</option><option value="$">$</option></select>
        </span></td>
	</tr>   
</table>                         
	  <input name="reset" type="reset" id="reset" onClick="location.href='<?php echo get_home_url(); ?>'" value="Indietro" nome="reset">
      <input name="reset" type="reset" value="Reset" />
      <input name="upload" type="submit" value="Conferma" />
</form>
			<?php get_sidebar('bottom'); ?>

<?php } else 
	{?>
	<div style="font-size: xx-large;font-weight: bold;margin-top: 20px;">
		Restricted area, please login
	</div>
	<?php
	}

get_footer(); ?>